<?php

namespace app\index\model;

use think\Model;

/**
 * 设备表
 * Class Device
 * @package app\index\model
 */
class Device extends Model
{
    public $autoWriteTimestamp = 'int';
    public static $DeviceChargeTime = 8; // 8小时
    protected $append = [
        'status_text',
        'category_id_text'
    ];

    /**
     * 获取设备最长使用时长配置数据，默认为9
     * @return int
     */
    public static function DeviceUseTime()
    {
        $rule = Rules::get(['name'=>'device_use_time']);
        return $rule ? $rule -> value : 9;
    }

    // 获取设备状态说明
    public function getStatusTextAttr($value,$data)
    {
        $status_list = ['', '使用中', '充电中', '空闲中', '安检中', '超时未安检', '已报废'];

        if (empty($data['status']) || empty($status_list[$data['status']])) return '';

        return $status_list[$data['status']];
    }

    // 获取柜门编号
    public function getDoorSerialAttr($value, $data)
    {
        if (empty($data['door_id'])) return '';
        $door = Door::get($data['door_id']);
        return $door ? $door->door_serial : '';
    }

    // 获取柜子编号
    public function getGearSerialAttr($value,$data)
    {
        if (empty($data['door_id'])) return '';
        $door = Door::get($data['door_id']);
        if (!$door) return '';
        $gear = db('gear')->where('id', $door->gear_id)->value('gear_serial');
        return empty($gear) ? '' : $gear;
    }

    // 设备类别
    public function getCategoryIdTextAttr($value,$data)
    {
        if (empty($data['category_id'])) return '';
        $category = DeviceCategory::get($data['category_id']);
        if (!$category) return '';
        return $category->getAttr('name');
    }
}